package com.cuit.bysj.InterfaceAction;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cuit.bysj.entities.Item;
import com.cuit.bysj.service.itemService;
import com.cuit.bysj.utils.SelectUtils;
import com.opensymphony.xwork2.ActionSupport;

public class androidItemAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private itemService itemservice;
	private String content;
	private String id;
	private Map<String, Object> dataMap;
	private String percentArr;

	public String getPercentArr() {
		return percentArr;
	}

	public void setPercentArr(String percentArr) {
		this.percentArr = percentArr;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setItemservice(itemService itemservice) {
		this.itemservice = itemservice;
	}

	public String addItem() {
		Item item = new Item(content);

		itemservice.addItem(item);

		dataMap = new HashMap<String, Object>();
		dataMap.put("code", "添加成功!");

		return "json";
	}

	public String delItem() {

		itemservice.delete(Integer.parseInt(id));

		dataMap = new HashMap<String, Object>();
		dataMap.put("code", "删除成功！");

		return "json";
	}

	public String updateItem() throws UnsupportedEncodingException {
		content = URLDecoder.decode(content, "UTF-8");

		Item item = itemservice.findByid(Integer.parseInt(id));
		item.setContent(content);
		itemservice.update(item);

		dataMap = new HashMap<String, Object>();
		dataMap.put("code", "修改成功！");

		return "json";
	}

	public String toggleState() {
		String code;

		if (itemservice.isOpen()) {
			itemservice.updateState(false);
			code = "评教关闭成功！";
		} else {
			itemservice.updateState(true);
			code = "评教开启成功！";
		}

		dataMap = new HashMap<String, Object>();
		dataMap.put("code", code);

		return "json";
	}

	public String queryState() {
		dataMap = new HashMap<String, Object>();
		dataMap.put("state", itemservice.isOpen());

		return "json";
	}

	public String saveItempercent() {
		dataMap = new HashMap<String, Object>();
		
		List<String> list = SelectUtils.savePercent(percentArr);
		List<Item> list1 = itemservice.itemlist();

		for (int i = 0; i < list.size(); i++) {
			Item item = list1.get(i);
			item.setPercent(Integer.parseInt(list.get(i)));
			itemservice.update(item);
		}
		
		dataMap.put("code", "修改指标成功！");

		return "json";
	}
}
